package com.badlogic.androidgames.mrnom;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.badlogic.androidgames.framework.FileIO;

/**
 * @deprecated:用于储存我们的设置，以及加载和保存它们
 * @author frank
 * @date 2015年12月5日
 */
public class Settings {
	/**音效是否播放**/
	public static boolean soundEnabled = true;
	/**前五名的高分**/
	public static int[] highscores = new int[] {100, 80, 50, 30, 10};
	/**从.mrnom的外部存储文件中加载设置项**/
	public static void load(FileIO files) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(files.readFile(".mrnom")));
			soundEnabled = Boolean.parseBoolean(in.readLine());
			for (int i = 0; i < 5; i++) {
				highscores[i] = Integer.parseInt(in.readLine());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (NumberFormatException e) {
			// 
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				// 
			}
		}
	}
	/**获取当前设置并将其转化为字符串存储到外部存储器上的.mrnom文件中**/
	public static void save(FileIO files) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(files.writeFile(".mrnom")));
			out.write(Boolean.toString(soundEnabled));
			for (int i = 0; i < 5; i++) {
				out.write(Integer.toString(highscores[i]));
			}
		} catch(IOException e) {
			
		} finally {
			try {
				if (out != null)
					out.close();
			} catch(IOException e) {
				
			}
		}
	}
	/**添加新的得分到高分榜，自动排序**/
	public static void addScore(int score) {
		for (int i = 0; i < 5; i++) {
			if (highscores[i] < score) {
				for (int j = 4; j < i; j--) {
					highscores[j] = highscores[j - 1];
				}
				highscores[i] = score;
				break;
			}
		}
	}
}
